<?php

namespace app\admin\controller;

use think\facade\Db;
use app\common\logic\wechat\WechatUtil;
use app\common\logic\WechatLogic;
use think\facade\View;

class Wechat extends Base
{
    private $wx_user;

    function __construct()
    {
        parent::__construct();
        $this->wx_user = Db::name('wx_set')->where('1=1')->find();
    }

    public function index()
    {
        $wx_user = $this->wx_user;
        header("Location:".url('Wechat/setting',['id'=>$wx_user['id']]));
        exit;
    }

    public function setting()
    {
        $id = input('id');
        if (!empty($id)) {
            $wechat = Db::name('wx_set')->where(array('id' => $id))->find();

            if (IS_POST) {
                $post_data = input('');
                $data = [];
                $data['appid'] = $post_data['appid'];
                $data['appsecret'] = $post_data['appsecret'];
                $data['qrcode'] = $post_data['qrcode'];
                $data['token'] = $post_data['token'];
                $data['encode'] = $post_data['encode'];
                $data['web_expires'] = 0;
                $data['create_time'] = time();
                if($id){
                    $row = Db::name('wx_set')->where(array('id' => $id))->update($data);
                }else{
                    $row = Db::name('wx_set')->insertGetId($data);
                }
                return json(['status'=>1,'msg'=>'修改成功']);
            }

        }else{
            $wechat = $this->wx_user;
        }
        $domain = getCurUrl();
        $web_info = [
            'ip' => $_SERVER['REMOTE_ADDR'],
            'https_url' => $domain,
            'wechat_url' => $domain.'/home/wechat/index',
        ];
        View::assign('info', $wechat);
        View::assign('web_info', $web_info);
        return View::fetch();
    }

    public function menu()
    {
        $wechat = $this->wx_user;
        if (empty($wechat)) {
            $this->error('请先在公众号配置添加公众号，才能进行微信菜单管理', U('Wechat/index'));
        }
        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            if(input('pid')){
                $map['pid'] = input('pid');
            }else{
                $map['pid'] = 0;
            }
            $count = Db::name('wx_menu')->alias('p')->where($map)->count();

            $field = 'p.*';
            $list = Db::name('wx_menu')
                ->alias('p')
                ->field($field)
                ->where($map)
                ->order('p.id desc ')
                ->page($page,$limit)
                ->select();
            foreach($list as $k=>$v){
                $children = Db::name('wx_menu')->alias('p')->where(['pid'=>$v['id']])->select();
                $list[$k]['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
                $list[$k]['children'] = $children;
            }
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list]);
        }

        return View::fetch('menu_new');
    }

    //一级菜单最多三个，二级菜单最多五个
    public function edit_menu(){
        $id = input('id');
        $info = Db::name('wx_menu')->where('id',$id)->find();
        if(IS_POST){
            $params = input();
            $data = [];
            $data['pid'] = $params['pid'];
            $data['name'] = $params['name'];
            $data['menu_type'] = $params['menu_type'];
            $data['message_type'] = $params['message_type'];
            $data['news_content'] = $params['news_content'];
            $data['text_content'] = $params['text_content'];
            $data['image_content'] = $params['image_content'];
            $data['voice_content'] = $params['voice_content'];
            $data['video_content'] = $params['video_content'];
            $data['video_title'] = $params['video_title'];
            $data['video_introduction'] = $params['video_introduction'];
            $data['menu_url'] = $params['menu_url'];
            $data['menu_appid'] = $params['menu_appid'];
            $data['menu_pagepath'] = $params['menu_pagepath'];
            if($id){
                Db::name('wx_menu')->where('id',$id)->update($data);
            }else{
                Db::name('wx_menu')->insert($data);
            }
            return json(['status'=>1,'msg'=>'修改成功！']);
        }else{
            $this->assign('info',$info);
            $parent_arr = Db::name('wx_menu')->where('pid',0)->select();
            $this->assign('parent_arr',$parent_arr);
            return $this->fetch();
        }
    }


    /*
     * 删除菜单
     */
    public function del_menu()
    {
        $id = input('id');
        if(!$id){
            exit('fail');
        }
        $row = Db::name('wx_menu')->where(array('id'=>$id))->delete();
        $row && Db::name('wx_menu')->where(array('pid'=>$id))->delete(); //删除子类
        if($row){
            exit('success');
        }else{
            exit('fail');
        }
    }

    /*
     * 生成微信菜单
     */
    public function pub_menu()
    {
        //获取父级菜单
        $p_menus = Db::name('wx_menu')->where(array('pid' => 0))->order('id ASC')->select();
        foreach($p_menus as $k=>$v){
            $sub_button = Db::name('wx_menu')->where('pid',$v['id'])->select();
            $p_menus[$k]['sub_button'] = $sub_button;
        }
        if (!count($p_menus) > 0) {
            $this->error('没有菜单可发布', U('Wechat/menu'));
        }

        $post = $this->convert_menu($p_menus);
        //dump($post);exit;
        $wechatObj = new WechatUtil($this->wx_user);
        if ($wechatObj->createMenu($post) === false) {
            $this->error($wechatObj->getError());
        }

        $this->success('菜单已成功生成', U('Wechat/menu'));
    }

    //菜单转换

    /**
     * @param $menudata
     * @return array[]|\think\response\Json
    CREATE TABLE `sy_mp_material` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `aid` int(11) DEFAULT NULL,
    `url` varchar(255) DEFAULT NULL,
    `media_id` varchar(255) DEFAULT NULL,
    `createtime` int(11) DEFAULT NULL,
    `type` varchar(255) DEFAULT NULL,
    `description` varchar(255) DEFAULT NULL,
    `endtime` int(11) DEFAULT '2147483647',
    PRIMARY KEY (`id`) USING BTREE,
    KEY `aid` (`aid`) USING BTREE,
    KEY `media_id` (`media_id`) USING BTREE,
    KEY `type` (`type`) USING BTREE
    ) ENGINE=InnoDB AUTO_INCREMENT=44 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
     */
    private function convert_menu($menudata)
    {
        $wechatObj = new WechatUtil($this->wx_user);
        $buttondata = [];
        foreach($menudata as $k=>$v){
            $newval = [];
            $newval['name'] = $v['name'];
            if($v['sub_button']){
                $sub_button = [];
                foreach($v['sub_button'] as $subv){
                    $newsubval = [];
                    $newsubval['name'] = $subv['name'];
                    if($subv['menu_type'] == 'click'){
                        if($subv['message_type']=='news'){
                            $newsubval['type'] = 'media_id';
                            $newsubval['media_id'] = $subv['news_content'];
                        }elseif($subv['message_type']=='image'){
                            $newsubval['type'] = 'media_id';
                            $newsubval['media_id'] = $wechatObj->getmediaid($subv['image_content']);
                        }elseif($subv['message_type']=='voice'){
                            $newsubval['type'] = 'media_id';
                            $newsubval['media_id'] = $wechatObj->getmediaid($subv['voice_content'],'voice');
                        }elseif($subv['message_type']=='video'){
                            $newsubval['type'] = 'media_id';
                            if(!$subv['video_title'] || !$subv['video_introduction']){
                                return json(['status'=>0,'msg'=>'请填写视频标题及描述']);
                            }
                            $newsubval['media_id'] = $wechatObj->getmediaid($subv['video_content'],'video',jsonEncode(['title'=>$subv['video_title'],'introduction'=>$subv['video_introduction']]));
                        }elseif($subv['message_type']=='text'){
                            $newsubval['type'] = 'click';
                            $newsubval['key'] = uniqid().'_'.rand(1000,9999);
                            Db::name('wx_menu')->insert(['key'=>$newsubval['key'],'val'=>$subv['text_content']]);
                        }
                    }elseif($subv['menu_type']=='view'){
                        $newsubval['type'] = 'view';
                        $newsubval['url'] = $subv['menu_url'];
                    }elseif($subv['menu_type']=='miniprogram'){
                        $newsubval['type'] = 'miniprogram';
                        $newsubval['appid'] = $subv['menu_appid'];
                        $newsubval['pagepath'] = $subv['menu_pagepath'];
                        $newsubval['url'] = request()->domain();
                    }
                    $sub_button[] = $newsubval;
                }
                $newval['sub_button'] = $sub_button;
            }else{
                if($v['menu_type'] == 'click'){
                    if($v['message_type']=='news'){
                        $newval['type'] = 'media_id';
                        $newval['media_id'] = $v['news_content'];
                    }elseif($v['message_type']=='image'){
                        $newval['type'] = 'media_id';
                        $newval['media_id'] = $wechatObj->getmediaid($v['image_content']);
                    }elseif($v['message_type']=='voice'){
                        $newval['type'] = 'media_id';
                        $newval['media_id'] = $wechatObj->getmediaid($v['voice_content'],'voice');
                    }elseif($v['message_type']=='video'){
                        $newval['type'] = 'media_id';
                        if(!$v['video_title'] || !$v['video_introduction']){
                            return json(['status'=>0,'msg'=>'请填写视频标题及描述']);
                        }
                        $newval['media_id'] = $wechatObj->getmediaid($v['video_content'],'video',jsonEncode(['title'=>$v['video_title'],'introduction'=>$v['video_introduction']]));
                    }elseif($v['message_type']=='text'){
                        $newval['type'] = 'click';
                        $newval['key'] = uniqid().'_'.rand(1000,9999);
                        Db::name('mp_menukey')->insert(['key'=>$newval['key'],'val'=>$v['text_content']]);
                    }
                }elseif($v['menu_type']=='view'){
                    $newval['type'] = 'view';
                    $newval['url'] = $v['menu_url'];
                }elseif($v['menu_type']=='miniprogram'){
                    $newval['type'] = 'miniprogram';
                    $newval['appid'] = $v['menu_appid'];
                    $newval['pagepath'] = $v['menu_pagepath'];
                    $newval['url'] = request()->domain();
                }
            }
            $buttondata[] = $newval;
        }

        return array('button'=>$buttondata);
    }

    /**
     * 自动回复的菜单
     */
    private function auto_reply_menu()
    {
        return [
            WxReply::TYPE_KEYWORD => ['menu' => '关键词自动回复', 'url' => url('auto_reply', ['type' => WxReply::TYPE_KEYWORD])],
            WxReply::TYPE_DEFAULT => ['menu' => '消息自动回复', 'url' => url('auto_reply_edit', ['type' => WxReply::TYPE_DEFAULT])],
            WxReply::TYPE_FOLLOW  => ['menu' => '关注时自动回复', 'url' => url('auto_reply_edit', ['type' => WxReply::TYPE_FOLLOW])]
        ];
    }

    /**
     * 自动回复展示
     */
    public function auto_reply()
    {
        $type = input('type', WxReply::TYPE_KEYWORD);
        $types = $this->auto_reply_menu();
        if (!key_exists($type, $types)) {
            $this->error("标签 $type 不存在");
        }
        $this->assign('type', $type);
        $this->assign('types', $types);

        if ($type == WxReply::TYPE_KEYWORD) {
            if(IS_AJAX){
                $page = input('page');
                $limit = input('limit')?:15;
                $map = [];
                $map['type'] = 'keyword';
                $count = Db::name('wx_reply')->where($map)->count();

                $field = '*';
                $list = Db::name('wx_reply')
                    ->field($field)
                    ->where($map)
                    ->order('id desc ')
                    ->page($page,$limit)
                    ->select();
                foreach($list as $k=>$v){
                    $list[$k]['update_time'] = date('Y-m-d H:i:s',$v['update_time']);
                }

                return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list]);
            }
            return $this->fetch('auto_replies_new');

        } else {
            $this->redirect('auto_reply_edit', ['type' => $type]);
        }
    }

    /**
     * 自动回复编辑页面
     */
    public function auto_reply_edit()
    {
        $id = input('id/d');
        $type = input('type', WxReply::TYPE_KEYWORD);
        $types = $this->auto_reply_menu();
        if (!key_exists($type, $types)) {
            $this->error("标签 $type 不存在");
        }
        $this->assign('type', $type);
        $this->assign('types', $types);

        if ($type == WxReply::TYPE_KEYWORD) {
            if ($id) {
                $reply = Db::name('wx_reply')->where('id',$id)->find();
                if(!$reply){
                    return ['status'=>0,'msg'=>'该自动回复不存在'];
                }
            }
        } else {
            $reply = Db::name('wx_reply')->where('type',$type)->find();
        }

        if  ( ! empty($reply)) {
            if ($reply->msg_type == WxReply::MSG_NEWS) {
                $news = WxMaterial::get($reply->material_id, 'wxNews');
                $this->assign('news', $news);
            }
            $this->assign('reply', $reply);
        }

        return $this->fetch('auto_reply_edit_new');
    }

    /**
     * 新增自动回复
     */
    public function add_auto_reply()
    {
        $info = input();
        if($info['msgtype'] == 'text'){
            $info['content'] = $_POST['text'];
        }elseif($info['msgtype'] == 'image'){
            $image = $_POST['image'];
            $image['MediaId'] = \app\common\Wechat::getmediaid($image['url']);
            $info['content'] = jsonEncode($image);
        }elseif($info['msgtype'] == 'voice'){
            $voice = $_POST['voice'];
            $voice['MediaId'] = \app\common\Wechat::getmediaid($voice['url'],'voice');
            $info['content'] = jsonEncode($voice);
        }elseif($info['msgtype'] == 'video'){
            $video = $_POST['video'];
            $video['MediaId'] = \app\common\Wechat::getmediaid($video['url'],'video',jsonEncode(['title'=>$video['title'],'introduction'=>$video['description']]));
            $info['content'] = jsonEncode($video);
        }elseif($info['msgtype'] == 'music'){
            $music = $_POST['music'];
            $info['content'] = jsonEncode($music);
        }elseif($info['msgtype'] == 'news'){
            $news = $_POST['news'];
            $info['content'] = jsonEncode($news);
        }
        if($info['id']){
            Db::name('wx_reply')->where('id',$info['id'])->update($info);
        }else{
            $info['createtime'] = time();
            $id = Db::name('wx_reply')->insertGetId($info);
        }
        return json(['status'=>1,'msg'=>'操作成功','url'=>(string)url('index')]);
    }

    /**
     * 更新自动回复
     */
    public function update_auto_reply()
    {
        $type = input('msg_type');
        $id = input('id/d', 0);
        $data = input('post.');

        $logic = new WechatLogic($this->wx_user);
        $return = $logic->updateAutoReply($type, $id, $data);
        $this->ajaxReturn($return);
    }

    /**
     * 删除自动回复
     */
    public function delete_auto_reply()
    {
        $id = input('id/d', 0);

        $logic = new WechatLogic($this->wx_user);
        $return = $logic->deleteAutoReply($id);
        $this->ajaxReturn($return);
    }

    /**
     * 粉丝详细列表
     */
    public function fans_list()
    {
        $keyword = input('keyword');
        $p = input('p/d');
        $num = 10;
        $logic = new WechatLogic;
        $return = $logic->getFanList($p, $num, $keyword);
        if ($return['status'] != 1) {
            $this->error($return['msg'], null, '', 100);
        }

        $texts = WxMaterial::all(['type' => WxMaterial::TYPE_TEXT]);
        $page  = new Page($return['result']['total'], $num);

        $this->assign('page', $page);
        $this->assign('texts', $texts);
        $this->assign('user_list', $return['result']['list']);
        return $this->fetch();
    }

    public function fan_info()
    {
        $openid = I('get.id');
        $wechatObj = new WechatUtil($this->wx_user);
        $list = $wechatObj->getFanInfo($openid);
        if ($list === false) {
            $this->error($wechatObj->getError());
        }

        $list['tags'] = $wechatObj->getFanTagNames($list['tagid_list']);
        if ($list['tags'] === false) {
            $this->error($wechatObj->getError());
        }

        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 处理发送的消息
     */
    public function send_text_msg()
    {
        $msg = I('post.msg');//内容
        $to_all = I('post.to_all', 0);//个体or全体
        $openids = I('post.openids');//个体id

        $wechatObj = new WechatUtil($this->wx_user);
        if ($to_all) {
            $result = $wechatObj->sendMsgToAll(0, 'text', $msg);
        } else {
            $result = $wechatObj->sendMsg($openids, 'text', $msg);
        }

        if ($result === false) {
            return $this->ajaxReturn(['status'=>0,'msg'=>$wechatObj->getError()]);
        }

        return $this->ajaxReturn(['status'=>1,'msg'=>'已发送！']);
    }

    /**
     * 素材管理
     */
    public function materials()
    {
        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            $count = Db::name('wx_material')->alias('p')->where($map)->count();

            $field = 'p.*';
            $list = Db::name('wx_material')
                ->alias('p')
                ->field($field)
                ->where($map)
                ->order('p.id desc ')
                ->page($page,$limit)
                ->select();
            foreach($list as $k=>$v){
                $list[$k]['upadate_time'] = date('Y-m-d H:i:s',$v['upadate_time']);
            }
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list]);
        }
        return $this->fetch();

        $tab = input('tab', 'news');
        $tabs = [
            'news' => '图文素材',
            'text' => '文本素材'
        ];
        if (!key_exists($tab, $tabs)) {
            $this->error("标签 $tab 不存在");
        }

        $p = input('p', 0);
        $num = 10;
        if ($tab == 'news') {
            $materials = WxMaterial::where(['type' => $tab])->with('wxNews')->order('update_time', 'desc')->page($p, $num)->select();
        } else {
            $materials = WxMaterial::where(['type' => $tab])->order('update_time', 'desc')->page($p, $num)->select();
        }

        $count = WxMaterial::where(['type' => $tab])->count();
        $page  = new Page($count, $num);

        $this->assign('page', $page);
        $this->assign('list', $materials);
        $this->assign('tab', $tab);
        $this->assign('tabs', $tabs);
        return $this->fetch('materials_'.$tab);
    }

    /**
     * 异步请求图文消息
     */
    public function ajax_news()
    {
        $p = input('p', 0);
        $num = 9;
        $materials = WxMaterial::where(['type' => WxMaterial::TYPE_NEWS])->with('wxNews')->order('update_time', 'desc')->page($p, $num)->select();
        $count = WxMaterial::where(['type' => WxMaterial::TYPE_NEWS])->count();
        $page  = new AjaxPage($count, $num);

        $this->assign('page', $page);
        $this->assign('list', $materials);
        return $this->fetch();
    }

    /**
     * 单图文素材编辑
     */
    public function news_edit()
    {
        $material_id = input('material_id/d');
        $news_id = input('news_id/d');

        if ($news_id) {
            if (!$news = WxNews::get(['id' => $news_id, 'material_id' => $material_id])) {
                $this->error('该图文素材不存在');
            }
            $this->assign('info', $news);
        }

        return $this->fetch('news_edit_new');
    }

    /**
     * 删除素材
     */
    public function delete_news()
    {
        $material_id = input('material_id/d');

        $logic = new WechatLogic($this->wx_user);
        $return = $logic->deleteNews($material_id);

        return $this->ajaxReturn($return);
    }

    /**
     * 删除多图文中的单图文
     */
    public function delete_single_news()
    {
        $news_id = input('news_id/d');

        $logic = new WechatLogic($this->wx_user);
        $return = $logic->deleteSingleNews($news_id);

        return $this->ajaxReturn($return);
    }

    /**
     * 新增或更新单图文素材
     */
    public function handle_news()
    {
        $material_id = input('material_id/d');//为0新增多素材，否则更新多素材
        $news_id = input('news_id/d', 0);//为0新增单素材，否则更新单素材，此时material_id不为0
        $data = input('post.');

        $result = $this->validate($data, 'WechatNews', [], true);
        if ($result !== true) {
            $this->ajaxReturn(['status' => 0, 'msg' => '参数错误', 'result' => $result]);
        }

        $logic = new WechatLogic;
        $return = $logic->createOrUpdateNews($material_id, $news_id, $data);
        return $this->ajaxReturn($return);
    }

    /**
     * 发送图文素材消息
     */
    public function send_news_msg()
    {
        $material_id = input('material_id');
        $to_all = input('to_all', 0);//个体or全体
        $openids = input('openids');//个体id

        $logic = new WechatLogic($this->wx_user);
        $return = $logic->sendNewsMsg($material_id, $openids, $to_all);
        return $this->ajaxReturn($return);
    }

    /**
     * 编辑文本素材
     */
    public function text_edit()
    {
        $material_id = input('material_id/d');
        if ($material_id) {
            if (!$text = WxMaterial::get(['id' => $material_id, 'type' => WxMaterial::TYPE_TEXT])) {
                $this->error('该文本素材不存在');
            }
            $this->assign('info', $text);
        }

        return $this->fetch();
    }

    /**
     * 新增或更新文本素材
     */
    public function handle_text()
    {
        $material_id = input('material_id/d');//为0新增素材，否则更新素材
        $data = input('post.');

        $logic = new WechatLogic;
        $return = $logic->createOrUpdateText($material_id, $data);
        return $this->ajaxReturn($return);
    }

    /**
     * 删除文本素材
     */
    public function delete_text()
    {
        $material_id = input('material_id/d');

        $logic = new WechatLogic($this->wx_user);
        $return = $logic->deleteText($material_id);

        return $this->ajaxReturn($return);
    }

    /**
     * 模板消息
     */
    public function template_msg()
    {
        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            $count = Db::name('wx_tpl_msg')->alias('p')->where($map)->count();

            $field = 'p.*';
            $list = Db::name('wx_tpl_msg')
                ->alias('p')
                ->field($field)
                ->where($map)
                ->order('p.id desc ')
                ->page($page,$limit)
                ->select();
            foreach($list as $k=>$v){
                $list[$k]['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
            }
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list]);
        }
        return $this->fetch('template_msg_new');
    }
    public function edit_tpl_msg(){
        $id = input('id');
        if(IS_POST){
            $params = input();
            $data = [];
            $data['title'] = $params['title']??'';
            $data['template_sn'] = $params['template_sn'];
            $data['template_id'] = $params['template_id'];
            $data['remark'] = $params['remark'];
            $data['type'] = $params['type'];
            $data['w_time'] = $params['w_time'];
            $data['status'] = $params['status'];
            if($id){
                Db::name('wx_tpl_msg')->where('id',$id)->update($data);
            }else{
                Db::name('wx_tpl_msg')->insert($data);
            }
        }else{
            $info = Db::name('wx_tpl_msg')->where('id',$id)->find();
            $this->assign('info',$info);
            return $this->fetch();
        }
    }

    /**
     * 设置模板消息
     */
    public function set_template_msg()
    {
        $template_sn = input('template_sn');
        $is_use = input('is_use/d');
        $remark = input('remark');

        $data = [];
        !is_null($is_use) && $data['is_use'] = $is_use;
        !is_null($remark) && $data['remark'] = $remark;

        $logic = new WechatLogic;
        $return = $logic->setTemplateMsg($template_sn, $data);
        $this->ajaxReturn($return);
    }

    /**
     * 重置模板消息
     */
    public function reset_template_msg()
    {
        $template_sn = input('template_sn');

        $logic = new WechatLogic;
        $return = $logic->resetTemplateMsg($template_sn);

        $this->ajaxReturn($return);
    }

    //上传公众号接口校验文件
    public function uploadjstxt(){
        if (empty($_FILES['file']['tmp_name'])) {
            return json(['status'=>0,'msg'=>'请选择文件']);
        }
        if ($_FILES['file']['type'] != 'text/plain') {
            return json(['status'=>0,'msg'=>'文件类型错误']);
        }
        $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
        if(!preg_match('/^[A-Za-z0-9]+$/', file_get_contents($_FILES['file']['tmp_name']))){
            return json(['status'=>0,'msg'=>'上传文件不合法,请重新上传']);
        }
        if ('txt' == strtolower($ext)) {
            $file = $_FILES['file'];
            $file['name'] = $this->parse_path($file['name']);
            if (is_uploaded_file($file['tmp_name'])) {
                move_uploaded_file($file['tmp_name'], ROOT_PATH . '/' . $file['name']);
            } else {
                rename($file['tmp_name'], ROOT_PATH . '/' . $file['name']);
            }
        }else{
            return json(['status'=>0,'msg'=>'上传文件不合法,请重新上传']);
        }

        return json(['status'=>1,'msg'=>'上传成功']);
    }
}